This notebook will demonstrate the process of using a deep neural network in R to perform image classification.

Necessary libraries:

In [1]:
library(gdata)
library("jpeg")
library("imager")
gdata: Unable to locate valid perl interpreter
gdata: 
gdata: read.xls() will be unable to read Excel XLS and XLSX files
gdata: unless the 'perl=' argument is used to specify the location of a
gdata: valid perl intrpreter.
gdata: 
gdata: (To avoid display of this message in the future, please ensure
gdata: perl is installed and available on the executable search path.)
gdata: Unable to load perl libaries needed by read.xls()
gdata: to support 'XLX' (Excel 97-2004) files.

gdata: Unable to load perl libaries needed by read.xls()
gdata: to support 'XLSX' (Excel 2007+) files.

gdata: Run the function 'installXLSXsupport()'
gdata: to automatically download and install the perl
gdata: libaries needed to support Excel XLS and XLSX formats.

Attaching package: 'gdata'

The following object is masked from 'package:stats':

    nobs

The following object is masked from 'package:utils':

    object.size

The following object is masked from 'package:base':

    startsWith

Loading required package: magrittr

Attaching package: 'imager'

The following object is masked from 'package:magrittr':

    add

The following objects are masked from 'package:stats':

    convolve, spectrum

The following object is masked from 'package:graphics':

    frame

The following object is masked from 'package:base':

    save.image

We use the following function to load the images:

In [2]:
getFileNameAndExtension <- function(file){ 
    ex <- strsplit(basename(file), split="\\.")
    ex<-unlist(ex)
    return(ex)
}
In [3]:
firstVideoFileName<-'marker.mov'
secondVideoFileName<-'bottle.mov'
outFilePrefix='out'
outFileSuffix='.jpg'
thumbNailFolder<-"Small"
In [4]:
nameAndExtension<-getFileNameAndExtension(firstVideoFileName)
fnames <- paste0(thumbNailFolder,nameAndExtension[[1]],"/","out", 2, ".jpg")
fnames
'Smallmarker/out2.jpg'
In [5]:
sampleMarker<-readJPEG(fnames,native=TRUE)
plot(0:1,0:1,type="n",ann=FALSE,axes=FALSE)
rasterImage(sampleMarker,0,0,1,1)
In [6]:
nameAndExtension<-getFileNameAndExtension(secondVideoFileName)
fnames <- paste0(thumbNailFolder,nameAndExtension[[1]],"/","out", 2, ".jpg")
fnames
'Smallbottle/out2.jpg'
In [7]:
sampleBottle<-readJPEG(fnames,native=TRUE)
plot(0:1,0:1,type="n",ann=FALSE,axes=FALSE)
rasterImage(sampleBottle,0,0,1,1)

We will use the grayscale intensity of each image as the feature input for a neural network.

In [8]:
dim(sampleMarker)
  1. 48
  2. 27
In [9]:
gs.matrix<-as.matrix(sampleMarker)
dim(gs.matrix)
  1. 48
  2. 27
In [10]:
i.vector<-unmatrix(gs.matrix,byrow=T)
length(i.vector) # unmatrix the gray intensity image as a single column vector, which will be used as feature values to train neural network
1296

Next we label the images with the following function:

In [11]:
loadImagesfromFolderAndLabel <- function(pathName,prefix, clasLabel) {
imageNames<-paste0(prefix,"*.*")
files<-list.files(path=pathName,pattern=imageNames,all.files=T,full.name=T,no..=T)
list_of_images=lapply(files,load.image)
#list_of_images=lapply(files,read.pnm)
#plot(list_of_images[[1]])
n.images<-length(list_of_images)
thmb.matrix<-as.matrix(list_of_images[[1]])
image.vector<-unmatrix(thmb.matrix,byrow=T)
for(ii in 2:n.images)
{
  thmb.matrix<-as.matrix(list_of_images[[ii]])
  i.vector<-unmatrix(thmb.matrix,byrow=T)
  image.vector<-rbind(image.vector,i.vector)
}
image.frame<-data.frame(image.vector)
n.rows<-nrow(image.frame)
class1.label<-rep(clasLabel,n.rows)
image.frame<-cbind(image.frame,class1.label)
rm(list_of_images)
return (image.frame)
}

We then create the input vectors:

In [12]:
nameAndExtension<-getFileNameAndExtension(firstVideoFileName)
folderName<-paste0(thumbNailFolder,nameAndExtension[[1]])
folderName
'Smallmarker'
In [13]:
class1.frame <- loadImagesfromFolderAndLabel(folderName,outFilePrefix,1)
dim(class1.frame)
  1. 124
  2. 1297
In [14]:
nameAndExtension<-getFileNameAndExtension(secondVideoFileName)
folderName<-paste0(thumbNailFolder,nameAndExtension[[1]])
folderName
'Smallbottle'
In [15]:
class2.frame <- loadImagesfromFolderAndLabel(folderName,outFilePrefix,-1)
dim(class2.frame)
  1. 112
  2. 1297
In [16]:
total.frame<-rbind(class1.frame,class2.frame)
dim(total.frame)
  1. 236
  2. 1297

Then we create a training and test set, using 60% of the images for the training set and 40% for the test set:

In [17]:
train.index<-sample(nrow(total.frame),nrow(total.frame)*0.6)
training.set<-total.frame[train.index,]
training.set
r1.c1r1.c2r1.c3r1.c4r1.c5r1.c6r1.c7r1.c8r1.c9r1.c10...r27.c40r27.c41r27.c42r27.c43r27.c44r27.c45r27.c46r27.c47r27.c48class1.label
i.vector.570.8392157 0.8156863 0.8666667 0.8196078 0.8431373 0.8117647 0.6196078 0.6235294 0.5960784 0.5450980 ... 0.4627451 0.4039216 0.1254902 0.121568630.098039220.121568630.035294120.047058820.05882353 1
i.vector.9410.9019608 0.9019608 0.8941176 0.8862745 0.8784314 0.8745098 0.8666667 0.8666667 0.8784314 0.8431373 ... 0.2392157 0.2196078 0.2392157 0.250980390.274509800.301960780.278431370.254901960.27058824-1
i.vector.9610.9098039 0.9019608 0.8941176 0.8823529 0.8745098 0.8705882 0.8705882 0.8705882 0.8705882 0.8470588 ... 0.2352941 0.2274510 0.2392157 0.243137250.270588240.298039220.282352940.258823530.27058824-1
i.vector.580.8274510 0.8313725 0.8470588 0.8352941 0.8470588 0.6392157 0.6431373 0.6196078 0.5843137 0.5333333 ... 0.4901961 0.2745098 0.1019608 0.105882350.109803920.145098040.039215690.074509800.05490196 1
i.vector.4210.9137255 0.9098039 0.9019608 0.8941176 0.8823529 0.8745098 0.8705882 0.8666667 0.8588235 0.8549020 ... 0.2980392 0.2627451 0.2470588 0.231372550.219607840.203921570.196078430.211764710.23921569-1
i.vector.700.8666667 0.8431373 0.8392157 0.8352941 0.8705882 0.6627451 0.6392157 0.5843137 0.5647059 0.5254902 ... 0.4588235 0.2274510 0.1176471 0.098039220.129411760.090196080.054901960.058823530.05490196 1
i.vector.6810.9176471 0.9137255 0.9058824 0.8980392 0.8901961 0.8784314 0.8745098 0.8705882 0.8627451 0.8588235 ... 0.2509804 0.2156863 0.2274510 0.235294120.243137250.266666670.290196080.294117650.28235294-1
i.vector.680.8470588 0.8588235 0.8431373 0.8549020 0.8745098 0.7882353 0.6627451 0.6313725 0.5686275 0.5333333 ... 0.4549020 0.2901961 0.1019608 0.137254900.090196080.117647060.043137250.047058820.04705882 1
i.vector.7610.9137255 0.9098039 0.9019608 0.8941176 0.8823529 0.8745098 0.8705882 0.8666667 0.8509804 0.8470588 ... 0.2549020 0.1960784 0.2235294 0.235294120.254901960.282352940.274509800.254901960.26666667-1
i.vector.6610.9176471 0.9137255 0.9058824 0.8980392 0.8901961 0.8784314 0.8745098 0.8705882 0.8705882 0.8666667 ... 0.2588235 0.2117647 0.2196078 0.227450980.262745100.305882350.298039220.270588240.28235294-1
i.vector.1090.8392157 0.8666667 0.8509804 0.8784314 0.8039216 0.6627451 0.6313725 0.6000000 0.5333333 0.4980392 ... 0.1529412 0.1176471 0.1137255 0.121568630.105882350.054901960.043137250.058823530.05882353 1
i.vector.2410.9137255 0.9098039 0.9019608 0.8941176 0.8823529 0.8745098 0.8705882 0.8666667 0.8823529 0.8470588 ... 0.2666667 0.2117647 0.2196078 0.239215690.266666670.286274510.294117650.286274510.27843137-1
i.vector.4110.9098039 0.9058824 0.8980392 0.8901961 0.8784314 0.8705882 0.8627451 0.8588235 0.8588235 0.8549020 ... 0.2980392 0.2509804 0.2235294 0.235294120.243137250.207843140.196078430.239215690.27450980-1
i.vector.2710.9176471 0.9137255 0.9058824 0.8980392 0.8901961 0.8784314 0.8745098 0.8705882 0.8627451 0.8588235 ... 0.2470588 0.2627451 0.2078431 0.227450980.270588240.270588240.286274510.298039220.25882353-1
i.vector.1040.8392157 0.8705882 0.8196078 0.8509804 0.8274510 0.6784314 0.6235294 0.6313725 0.5568627 0.5294118 ... 0.2039216 0.1098039 0.1058824 0.117647060.105882350.054901960.039215690.054901960.05098039 1
i.vector.730.8392157 0.8392157 0.8549020 0.8470588 0.8588235 0.6470588 0.6431373 0.6117647 0.5803922 0.5019608 ... 0.4470588 0.2078431 0.1215686 0.101960780.133333330.105882350.062745100.058823530.07450980 1
i.vector.750.8392157 0.8392157 0.8549020 0.8470588 0.8588235 0.6470588 0.6431373 0.6117647 0.5725490 0.5176471 ... 0.4078431 0.1882353 0.1254902 0.105882350.117647060.094117650.070588240.070588240.06274510 1
i.vector.770.8352941 0.8352941 0.8470588 0.8392157 0.8549020 0.6470588 0.6509804 0.6235294 0.5568627 0.5176471 ... 0.6509804 0.1686275 0.1176471 0.113725490.129411760.101960780.070588240.066666670.06274510 1
i.vector.8310.9058824 0.9058824 0.8980392 0.8901961 0.8784314 0.8705882 0.8627451 0.8588235 0.8588235 0.8549020 ... 0.2470588 0.2313725 0.2352941 0.235294120.262745100.301960780.286274510.254901960.26274510-1
i.vector.5110.9176471 0.9137255 0.9058824 0.8980392 0.8901961 0.8784314 0.8745098 0.8705882 0.8588235 0.8549020 ... 0.2392157 0.2470588 0.2352941 0.219607840.223529410.258823530.290196080.282352940.25882353-1
i.vector.10.8666667 0.8196078 0.8745098 0.7921569 0.6470588 0.6274510 0.5921569 0.5529412 0.5176471 0.4941176 ... 0.1254902 0.1058824 0.1215686 0.109803920.062745100.035294120.050980390.062745100.05490196 1
i.vector.7810.9137255 0.9098039 0.9019608 0.8941176 0.8823529 0.8745098 0.8705882 0.8666667 0.8588235 0.8549020 ... 0.2235294 0.2000000 0.2039216 0.254901960.247058820.278431370.298039220.243137250.28235294-1
i.vector.40.8470588 0.8235294 0.8901961 0.7882353 0.6313725 0.6196078 0.5921569 0.5529412 0.5176471 0.4980392 ... 0.1176471 0.1019608 0.1372549 0.109803920.054901960.043137250.050980390.047058820.05490196 1
i.vector.430.8352941 0.8431373 0.8196078 0.8627451 0.8039216 0.6549020 0.6196078 0.5882353 0.5803922 0.5372549 ... 0.9058824 0.1176471 0.1019608 0.117647060.113725490.066666670.039215690.054901960.06274510 1
i.vector.20.8666667 0.8196078 0.8745098 0.7921569 0.6470588 0.6274510 0.5921569 0.5529412 0.5215686 0.4941176 ... 0.1058824 0.1058824 0.1254902 0.109803920.062745100.035294120.047058820.058823530.05098039 1
i.vector.10710.9098039 0.9019608 0.8941176 0.8823529 0.8745098 0.8705882 0.8705882 0.8705882 0.8901961 0.8470588 ... 0.2352941 0.1882353 0.2313725 0.250980390.254901960.278431370.282352940.266666670.27450980-1
i.vector.6110.9098039 0.9058824 0.8980392 0.8941176 0.8862745 0.8784314 0.8745098 0.8705882 0.8666667 0.8549020 ... 0.2666667 0.2588235 0.2549020 0.262745100.274509800.270588240.270588240.294117650.32549020-1
i.vector.7010.9176471 0.9137255 0.9058824 0.8980392 0.8901961 0.8784314 0.8745098 0.8705882 0.8588235 0.8549020 ... 0.2627451 0.2078431 0.2274510 0.227450980.235294120.262745100.262745100.258823530.27843137-1
i.vector.1060.8352941 0.8627451 0.8274510 0.8666667 0.8235294 0.6705882 0.6313725 0.6235294 0.5568627 0.4980392 ... 0.2313725 0.1137255 0.1058824 0.117647060.101960780.050980390.039215690.054901960.05490196 1
i.vector.1190.8705882 0.8392157 0.8627451 0.8117647 0.6627451 0.6156863 0.6274510 0.5803922 0.5019608 0.4862745 ... 0.1176471 0.1058824 0.1215686 0.109803920.062745100.035294120.050980390.062745100.05490196 1
................................. ..............................
i.vector.4410.9137255 0.9098039 0.9019608 0.8941176 0.8823529 0.8745098 0.8705882 0.8666667 0.8588235 0.8549020 ... 0.3019608 0.2352941 0.2588235 0.250980390.211764710.192156860.215686270.239215690.24313725-1
i.vector.490.8352941 0.8470588 0.8470588 0.8588235 0.6509804 0.6392157 0.6000000 0.5803922 0.5411765 0.5098039 ... 0.6980392 0.1647059 0.1098039 0.113725490.113725490.062745100.050980390.070588240.05098039 1
i.vector.440.8117647 0.8392157 0.8117647 0.8470588 0.7960784 0.6352941 0.5843137 0.5725490 0.5529412 0.5137255 ... 0.8705882 0.1254902 0.1176471 0.133333330.117647060.062745100.047058820.062745100.05490196 1
i.vector.30.8509804 0.8156863 0.8823529 0.7921569 0.6352941 0.6117647 0.5843137 0.5568627 0.5176471 0.4901961 ... 0.1176471 0.1098039 0.1254902 0.113725490.062745100.035294120.047058820.058823530.05098039 1
i.vector.120.8745098 0.8549020 0.8784314 0.8156863 0.6666667 0.6196078 0.6274510 0.5725490 0.5137255 0.4901961 ... 0.1294118 0.1058824 0.1254902 0.109803920.062745100.035294120.047058820.058823530.05098039 1
i.vector.600.8470588 0.8470588 0.8588235 0.8470588 0.8588235 0.6431373 0.6352941 0.6039216 0.5843137 0.5176471 ... 0.4627451 0.2313725 0.1019608 0.109803920.094117650.137254900.035294120.066666670.05882353 1
i.vector.5210.9137255 0.9098039 0.9019608 0.8941176 0.8823529 0.8745098 0.8705882 0.8666667 0.8549020 0.8509804 ... 0.2235294 0.2431373 0.2392157 0.266666670.231372550.262745100.305882350.262745100.29411765-1
i.vector.8610.9019608 0.9019608 0.8941176 0.8862745 0.8784314 0.8745098 0.8666667 0.8666667 0.8666667 0.8509804 ... 0.2509804 0.2156863 0.2352941 0.239215690.262745100.305882350.305882350.282352940.28235294-1
i.vector.810.8352941 0.8274510 0.8431373 0.8392157 0.8470588 0.6509804 0.6509804 0.5803922 0.5725490 0.5294118 ... 1.0000000 0.1215686 0.1098039 0.121568630.117647060.070588240.043137250.058823530.06666667 1
i.vector.830.8392157 0.8666667 0.8352941 0.8705882 0.8196078 0.6588235 0.6117647 0.6000000 0.5725490 0.5215686 ... 0.8901961 0.1176471 0.1098039 0.117647060.105882350.062745100.050980390.062745100.05098039 1
i.vector.910.8392157 0.8549020 0.8274510 0.8705882 0.8156863 0.6784314 0.6313725 0.5803922 0.5686275 0.5058824 ... 0.6745098 0.1098039 0.1058824 0.121568630.109803920.058823530.047058820.062745100.05882353 1
i.vector.6310.9176471 0.9137255 0.9019608 0.8941176 0.8862745 0.8823529 0.8784314 0.8784314 0.8588235 0.8588235 ... 0.2666667 0.2509804 0.2549020 0.266666670.274509800.282352940.286274510.290196080.29019608-1
i.vector.5310.9176471 0.9137255 0.9058824 0.8941176 0.8823529 0.8705882 0.8627451 0.8588235 0.8549020 0.8509804 ... 0.2274510 0.2039216 0.2156863 0.235294120.258823530.274509800.278431370.278431370.28235294-1
i.vector.500.8549020 0.8588235 0.8509804 0.8588235 0.6470588 0.6352941 0.6039216 0.5882353 0.5372549 0.5137255 ... 0.6784314 0.1686275 0.1176471 0.117647060.121568630.066666670.058823530.074509800.05490196 1
i.vector.1130.8313725 0.8431373 0.8509804 0.8705882 0.6627451 0.6509804 0.6078431 0.5882353 0.5294118 0.5098039 ... 0.1921569 0.1019608 0.1098039 0.121568630.094117650.047058820.047058820.062745100.05490196 1
i.vector.50.8627451 0.8470588 0.8980392 0.7843137 0.6431373 0.6235294 0.5882353 0.5686275 0.5176471 0.4941176 ... 0.1176471 0.1098039 0.1254902 0.113725490.062745100.035294120.047058820.058823530.05098039 1
i.vector.350.8352941 0.8549020 0.8352941 0.8392157 0.8627451 0.7803922 0.6470588 0.5921569 0.5960784 0.5372549 ... 0.4509804 0.9921569 0.1882353 0.043137250.121568630.133333330.054901960.062745100.03529412 1
i.vector.690.8470588 0.8392157 0.8470588 0.8431373 0.8705882 0.6627451 0.6509804 0.6117647 0.5686275 0.5294118 ... 0.4431373 0.2313725 0.1019608 0.129411760.109803920.133333330.039215690.082352940.05490196 1
i.vector.640.8549020 0.8470588 0.8509804 0.8431373 0.8627451 0.6509804 0.6352941 0.5960784 0.5568627 0.5176471 ... 0.4823529 0.2156863 0.1254902 0.098039220.125490200.105882350.062745100.050980390.06274510 1
i.vector.3010.9176471 0.9137255 0.9058824 0.8941176 0.8823529 0.8705882 0.8627451 0.8588235 0.8549020 0.8509804 ... 0.2352941 0.2156863 0.2156863 0.231372550.262745100.274509800.254901960.235294120.22745098-1
i.vector.4010.9098039 0.9058824 0.8980392 0.8901961 0.8784314 0.8705882 0.8627451 0.8588235 0.8549020 0.8509804 ... 0.2823529 0.2431373 0.2156863 0.223529410.235294120.211764710.207843140.250980390.27843137-1
i.vector.7210.9137255 0.9098039 0.9019608 0.8941176 0.8823529 0.8745098 0.8705882 0.8666667 0.8588235 0.8509804 ... 0.2627451 0.1960784 0.2313725 0.231372550.235294120.266666670.278431370.278431370.29019608-1
i.vector.710.8392157 0.8392157 0.8549020 0.8470588 0.8588235 0.6470588 0.6431373 0.6117647 0.5686275 0.5215686 ... 0.4274510 0.2156863 0.1294118 0.101960780.125490200.101960780.054901960.054901960.07450980 1
i.vector.3210.9137255 0.9098039 0.9019608 0.8901961 0.8784314 0.8666667 0.8588235 0.8549020 0.8549020 0.8509804 ... 0.2745098 0.2235294 0.2274510 0.231372550.243137250.247058820.243137250.231372550.21960784-1
i.vector.630.8549020 0.8470588 0.8509804 0.8431373 0.8627451 0.6509804 0.6352941 0.5960784 0.5725490 0.5294118 ... 0.4431373 0.2156863 0.1254902 0.098039220.125490200.105882350.062745100.050980390.06274510 1
i.vector.790.8470588 0.8509804 0.8196078 0.8352941 0.8666667 0.7882353 0.6705882 0.6392157 0.5607843 0.5333333 ... 0.4431373 0.2431373 0.1058824 0.109803920.098039220.145098040.035294120.054901960.03921569 1
i.vector.1140.8313725 0.8392157 0.8352941 0.8509804 0.6470588 0.6431373 0.6156863 0.6039216 0.5137255 0.5019608 ... 0.1764706 0.1098039 0.1058824 0.121568630.105882350.058823530.047058820.062745100.05882353 1
i.vector.10510.9019608 0.9019608 0.8941176 0.8862745 0.8784314 0.8745098 0.8666667 0.8666667 0.8823529 0.8470588 ... 0.2392157 0.2000000 0.2313725 0.250980390.270588240.278431370.258823530.258823530.29803922-1
i.vector.10210.9019608 0.9019608 0.8941176 0.8862745 0.8784314 0.8745098 0.8666667 0.8666667 0.8588235 0.8509804 ... 0.2235294 0.2078431 0.2313725 0.243137250.262745100.298039220.294117650.274509800.27843137-1
i.vector.300.8470588 0.8431373 0.8431373 0.8313725 0.8627451 0.6666667 0.6509804 0.5921569 0.5686275 0.5098039 ... 0.4117647 0.2039216 0.1254902 0.105882350.137254900.109803920.058823530.050980390.06274510 1
In [18]:
test.set<-total.frame[-train.index,]
test.set
r1.c1r1.c2r1.c3r1.c4r1.c5r1.c6r1.c7r1.c8r1.c9r1.c10...r27.c40r27.c41r27.c42r27.c43r27.c44r27.c45r27.c46r27.c47r27.c48class1.label
i.vector.70.8549020 0.8431373 0.8745098 0.8196078 0.6784314 0.6274510 0.6156863 0.5411765 0.5254902 0.4980392 ... 0.117647060.1137255 0.129411760.117647060.066666670.039215690.050980390.062745100.058823531
i.vector.90.8705882 0.8431373 0.8666667 0.8196078 0.6705882 0.6196078 0.6313725 0.5843137 0.5098039 0.4941176 ... 0.121568630.1254902 0.141176470.121568630.070588240.043137250.054901960.070588240.066666671
i.vector.100.8549020 0.8392157 0.8705882 0.8196078 0.6627451 0.6078431 0.6235294 0.5882353 0.5058824 0.4823529 ... 0.094117650.1254902 0.141176470.121568630.070588240.043137250.054901960.070588240.066666671
i.vector.110.8549020 0.8352941 0.8274510 0.8196078 0.8666667 0.6784314 0.6588235 0.5960784 0.5529412 0.5215686 ... 0.607843140.1843137 0.117647060.105882350.129411760.101960780.062745100.050980390.050980391
i.vector.140.8745098 0.8196078 0.8784314 0.8000000 0.6509804 0.6235294 0.5960784 0.5725490 0.5137255 0.4941176 ... 0.121568630.1176471 0.133333330.117647060.066666670.035294120.050980390.066666670.062745101
i.vector.150.8627451 0.8352941 0.8627451 0.8117647 0.6666667 0.6156863 0.6274510 0.5803922 0.5098039 0.4823529 ... 0.090196080.1176471 0.133333330.117647060.066666670.035294120.050980390.066666670.062745101
i.vector.170.8627451 0.8392157 0.8666667 0.8117647 0.6588235 0.6000000 0.6117647 0.5725490 0.4941176 0.4862745 ... 0.117647060.1176471 0.137254900.105882350.054901960.047058820.047058820.054901960.082352941
i.vector.220.8392157 0.8274510 0.8274510 0.8235294 0.8705882 0.6784314 0.6509804 0.5803922 0.5529412 0.5215686 ... 0.501960780.1764706 0.121568630.109803920.125490200.098039220.066666670.054901960.047058821
i.vector.240.8627451 0.8274510 0.8705882 0.8235294 0.6549020 0.6000000 0.6235294 0.5647059 0.5019608 0.4980392 ... 0.074509800.1098039 0.137254900.054901960.039215690.050980390.035294120.070588240.054901961
i.vector.250.8431373 0.8784314 0.8431373 0.8313725 0.6823529 0.5803922 0.6431373 0.5333333 0.5019608 0.5019608 ... 0.105882350.1254902 0.113725490.066666670.035294120.047058820.047058820.050980390.070588241
i.vector.260.8588235 0.8470588 0.8784314 0.8235294 0.6745098 0.6196078 0.6156863 0.5529412 0.5137255 0.4980392 ... 0.105882350.1137255 0.129411760.050980390.047058820.058823530.031372550.066666670.058823531
i.vector.270.8666667 0.8549020 0.8862745 0.8274510 0.6745098 0.6235294 0.6196078 0.5568627 0.5019608 0.4901961 ... 0.113725490.1137255 0.129411760.050980390.047058820.058823530.031372550.066666670.058823531
i.vector.280.8588235 0.8352941 0.8352941 0.8431373 0.8862745 0.6823529 0.6470588 0.5843137 0.5607843 0.5176471 ... 0.439215690.1921569 0.113725490.098039220.129411760.105882350.054901960.047058820.062745101
i.vector.310.8352941 0.8392157 0.8509804 0.8431373 0.8666667 0.6666667 0.6470588 0.5921569 0.5764706 0.5333333 ... 0.466666670.1882353 0.117647060.101960780.125490200.098039220.062745100.054901960.054901961
i.vector.390.8352941 0.8352941 0.8509804 0.8431373 0.8627451 0.6549020 0.6588235 0.6313725 0.5725490 0.5137255 ... 0.886274510.1137255 0.113725490.129411760.117647060.066666670.047058820.054901960.047058821
i.vector.410.8313725 0.8313725 0.8431373 0.8352941 0.8509804 0.6431373 0.6431373 0.6156863 0.5686275 0.5333333 ... 0.894117650.1176471 0.105882350.121568630.121568630.070588240.039215690.047058820.050980391
i.vector.460.8509804 0.8235294 0.8745098 0.8352941 0.8588235 0.8274510 0.6274510 0.6235294 0.5921569 0.5372549 ... 0.435294120.8784314 0.156862750.082352940.121568630.129411760.035294120.000000000.027450981
i.vector.470.8392157 0.8274510 0.8235294 0.8509804 0.7372549 0.6156863 0.6235294 0.5764706 0.5294118 0.5019608 ... 1.000000000.1254902 0.109803920.125490200.121568630.074509800.050980390.066666670.074509801
i.vector.510.8627451 0.8627451 0.8470588 0.8745098 0.6823529 0.6431373 0.5803922 0.5803922 0.5490196 0.5254902 ... 0.678431370.1686275 0.109803920.113725490.121568630.074509800.066666670.078431370.050980391
i.vector.520.8470588 0.8745098 0.8509804 0.8588235 0.7686275 0.6235294 0.6039216 0.5921569 0.5490196 0.5215686 ... 0.588235290.1843137 0.125490200.117647060.133333330.109803920.078431370.070588240.058823531
i.vector.540.8470588 0.8470588 0.8745098 0.8509804 0.8352941 0.6313725 0.6352941 0.5803922 0.5607843 0.5215686 ... 0.462745100.2117647 0.129411760.105882350.133333330.105882350.058823530.054901960.070588241
i.vector.560.8392157 0.8431373 0.8588235 0.8431373 0.8509804 0.6392157 0.6392157 0.6156863 0.6000000 0.5137255 ... 0.470588240.2705882 0.098039220.101960780.105882350.141176470.039215690.074509800.054901961
i.vector.610.8509804 0.8509804 0.8627451 0.8509804 0.8627451 0.6470588 0.6392157 0.6078431 0.5647059 0.5176471 ... 0.435294120.2156863 0.109803920.129411760.101960780.137254900.035294120.070588240.062745101
i.vector.660.8470588 0.8392157 0.8431373 0.8392157 0.8666667 0.6549020 0.6431373 0.6039216 0.5725490 0.5294118 ... 0.450980390.2117647 0.121568630.098039220.129411760.109803920.066666670.058823530.066666671
i.vector.720.8470588 0.8392157 0.8431373 0.8392157 0.8666667 0.6549020 0.6431373 0.6039216 0.5882353 0.5294118 ... 0.466666670.2196078 0.133333330.113725490.137254900.113725490.066666670.058823530.074509801
i.vector.740.8392157 0.8392157 0.8549020 0.8470588 0.8588235 0.6470588 0.6431373 0.6117647 0.5725490 0.5137255 ... 0.380392160.1921569 0.129411760.109803920.117647060.094117650.066666670.066666670.058823531
i.vector.760.8392157 0.8392157 0.8549020 0.8470588 0.8588235 0.6470588 0.6431373 0.6117647 0.5529412 0.5254902 ... 0.517647060.1843137 0.129411760.113725490.121568630.090196080.066666670.066666670.070588241
i.vector.800.8392157 0.8352941 0.8392157 0.8196078 0.8470588 0.6509804 0.6431373 0.5921569 0.5450980 0.5098039 ... 1.000000000.1490196 0.109803920.109803920.113725490.066666670.039215690.054901960.070588241
i.vector.880.8274510 0.8666667 0.8392157 0.8666667 0.8196078 0.6705882 0.6196078 0.5960784 0.5882353 0.4862745 ... 0.882352940.1137255 0.109803920.121568630.109803920.058823530.043137250.058823530.054901961
i.vector.920.8235294 0.8666667 0.8392157 0.8705882 0.8235294 0.6784314 0.6274510 0.6039216 0.5490196 0.4862745 ... 0.301960780.1137255 0.109803920.125490200.109803920.058823530.047058820.058823530.054901961
................................. ..............................
i.vector.2910.91372550.90980390.90196080.89411760.88235290.87450980.87058820.86666670.85490200.8509804... 0.23921570.21568630.21960780.22352940.23529410.24313730.24313730.23921570.2313725-1
i.vector.3110.90980390.90588240.89803920.89019610.87843140.87058820.86274510.85882350.85490200.8509804... 0.25882350.23529410.23137250.23529410.23921570.24705880.24705880.23921570.2313725-1
i.vector.3310.91372550.90980390.90196080.89019610.87843140.86666670.85882350.85490200.85882350.8549020... 0.28235290.22352940.22745100.23137250.23529410.23529410.23529410.23137250.2274510-1
i.vector.3610.91372550.90980390.90196080.89411760.88235290.87450980.87058820.86666670.85882350.8549020... 0.31372550.27843140.24313730.23921570.23921570.19607840.18039220.20784310.2313725-1
i.vector.3910.90980390.90588240.89803920.89019610.87843140.87058820.86274510.85882350.85490200.8509804... 0.29019610.24705880.20784310.23137250.23921570.19215690.20392160.25882350.2588235-1
i.vector.4610.91764710.91372550.90588240.89803920.89019610.87843140.87450980.87058820.85882350.8549020... 0.27058820.25882350.25490200.23137250.20000000.20784310.23921570.24705880.2274510-1
i.vector.4810.91764710.91372550.90588240.89803920.89019610.87843140.87450980.87058820.85490200.8509804... 0.25098040.27843140.23921570.20000000.19607840.22745100.25098040.25098040.2392157-1
i.vector.4910.91764710.91372550.90588240.89803920.89019610.87843140.87450980.87058820.85490200.8509804... 0.25490200.28235290.23921570.20000000.20784310.24313730.27058820.26274510.2392157-1
i.vector.5410.90196080.90196080.89411760.88627450.87843140.87450980.86666670.86666670.85098040.8470588... 0.22745100.20392160.21568630.23529410.25490200.27450980.28235290.29019610.2941176-1
i.vector.5610.90196080.90196080.89411760.88627450.87843140.87450980.86666670.86666670.85490200.8509804... 0.23529410.27843140.28235290.20000000.16862750.24313730.26274510.25490200.3215686-1
i.vector.5810.89411760.88627450.87843140.86666670.85882350.85490200.85490200.85490200.85490200.8274510... 0.26666670.26274510.24705880.26274510.30196080.30196080.27450980.29019610.3372549-1
i.vector.6010.90980390.90196080.89411760.88235290.87450980.87058820.87058820.87058820.87843140.8588235... 0.25098040.25490200.25490200.27058820.28627450.28235290.27843140.29411760.3176471-1
i.vector.7410.91372550.90980390.90196080.89411760.88235290.87450980.87058820.86666670.85882350.8549020... 0.27843140.21960780.23529410.24313730.24313730.25490200.27058820.27450980.2705882-1
i.vector.7510.91372550.90980390.90196080.89411760.88235290.87450980.87058820.86666670.85882350.8549020... 0.25098040.20392160.22352940.22745100.23921570.27058820.27450980.27450980.2941176-1
i.vector.7710.91372550.90980390.90196080.89411760.88235290.87450980.87058820.86666670.86666670.8588235... 0.26274510.21568630.23921570.24313730.24705880.27058820.27058820.26666670.2941176-1
i.vector.7910.90588240.90588240.89803920.89019610.87843140.87058820.86274510.85882350.85882350.8549020... 0.26666670.20392160.21960780.23137250.26666670.30588240.29019610.27058820.2823529-1
i.vector.8010.90588240.90588240.89803920.89019610.87843140.87058820.86274510.85882350.85882350.8509804... 0.25882350.20784310.22352940.23529410.27058820.30588240.29411760.27058820.2862745-1
i.vector.8110.90588240.90588240.89803920.89019610.87843140.87058820.86274510.85882350.85882350.8509804... 0.26274510.22352940.23137250.23529410.26666670.30588240.29019610.25882350.2666667-1
i.vector.8410.90196080.90196080.89411760.88627450.87843140.87450980.86666670.86666670.87843140.8627451... 0.23137250.20784310.21960780.23137250.26274510.30196080.29019610.27058820.2862745-1
i.vector.8810.90196080.90196080.89411760.88627450.87843140.87450980.86666670.86666670.87450980.8588235... 0.26274510.20000000.23921570.26274510.28235290.29411760.26666670.25490200.2901961-1
i.vector.8910.91372550.90980390.90196080.89411760.88235290.87450980.87058820.86666670.86274510.8549020... 0.22352940.20392160.19215690.24705880.24313730.25490200.28627450.24705880.2745098-1
i.vector.9010.90196080.90196080.89411760.88627450.87843140.87450980.86666670.86666670.87450980.8588235... 0.25490200.20784310.23529410.24313730.26666670.30196080.29411760.27058820.2705882-1
i.vector.9110.90196080.90196080.89411760.88627450.87843140.87450980.86666670.86666670.87450980.8588235... 0.25490200.20392160.22352940.23921570.26666670.30196080.28627450.27058820.2941176-1
i.vector.9510.90196080.90196080.89411760.88627450.87843140.87450980.86666670.86666670.87843140.8627451... 0.23529410.21176470.23137250.26274510.28627450.28235290.26274510.26666670.2862745-1
i.vector.9910.90980390.90588240.89803920.89019610.87843140.87058820.86274510.85882350.86666670.8588235... 0.20784310.20000000.21960780.23529410.26274510.29411760.28235290.26666670.2862745-1
i.vector.10010.91764710.91372550.90588240.89803920.89019610.87843140.87450980.87058820.86666670.8627451... 0.21568630.18823530.21176470.23529410.25098040.26274510.27450980.27058820.2549020-1
i.vector.10110.90980390.90196080.89411760.88235290.87450980.87058820.87058820.87058820.85882350.8509804... 0.20784310.20784310.22352940.23137250.25882350.29019610.28235290.27058820.2941176-1
i.vector.10410.90196080.89803920.88627450.87843140.87058820.86666670.86666670.86666670.89019610.8588235... 0.23137250.21568630.23137250.23921570.26274510.29411760.28235290.27058820.2941176-1
i.vector.10610.90196080.90196080.89411760.88627450.87843140.87450980.86666670.86666670.87450980.8588235... 0.22352940.19607840.22352940.23529410.25098040.28235290.28235290.27058820.2862745-1
i.vector.10810.90980390.90196080.89411760.88235290.87450980.87058820.87058820.87058820.86274510.8470588... 0.22745100.19607840.20784310.24313730.28627450.30196080.29019610.27450980.2705882-1

Next we train the neural network, using one hidden layer, 20 hidden nodes, and 500 repetitions:

In [19]:
library(neuralnet)
myform <- as.formula(paste('class1.label ~ ',paste(names(training.set[!names(training.set) %in% 'class1.label']), collapse = ' + ')))
oneHiddenLayerClassifier <- neuralnet(myform, training.set, hidden = 20, rep=500, linear.output = FALSE, threshold = 0.01)
summary(oneHiddenLayerClassifier)
                    Length   Class      Mode    
call                       7 -none-     call    
response                 141 -none-     numeric 
covariate             182736 -none-     numeric 
model.list                 2 -none-     list    
err.fct                    1 -none-     function
act.fct                    1 -none-     function
linear.output              1 -none-     logical 
data                    1297 data.frame list    
exclude                    0 -none-     NULL    
net.result               500 -none-     list    
weights                  500 -none-     list    
generalized.weights      500 -none-     list    
startweights             500 -none-     list    
result.matrix       12982000 -none-     numeric 

Next we make classification predictions:

In [20]:
class.index<-length(test.set)
predictionResults<-compute(oneHiddenLayerClassifier,test.set[,-class.index])
predictionResults$net.result
i.vector.70.9902013
i.vector.90.9888197
i.vector.100.9912318
i.vector.110.9924009
i.vector.140.9914549
i.vector.150.9920879
i.vector.170.9911919
i.vector.220.9922836
i.vector.240.9911527
i.vector.250.9899471
i.vector.260.9841650
i.vector.270.9883128
i.vector.280.9924138
i.vector.310.9921908
i.vector.390.9926283
i.vector.410.9926662
i.vector.460.9918845
i.vector.470.9917454
i.vector.510.9897318
i.vector.520.9923689
i.vector.540.9924310
i.vector.560.9924591
i.vector.610.9924227
i.vector.660.9925855
i.vector.720.9922542
i.vector.740.9925564
i.vector.760.9925309
i.vector.800.9926041
i.vector.880.9924797
i.vector.920.9911323
......
i.vector.2911.131319e-04
i.vector.3111.119527e-04
i.vector.3311.016458e-04
i.vector.3611.054260e-04
i.vector.3911.019552e-04
i.vector.4611.012064e-04
i.vector.4811.020088e-04
i.vector.4911.017755e-04
i.vector.5419.778543e-05
i.vector.5611.853237e-04
i.vector.5811.002290e-04
i.vector.6011.005847e-04
i.vector.7419.915155e-05
i.vector.7511.000045e-04
i.vector.7711.084560e-04
i.vector.7911.083800e-04
i.vector.8011.077891e-04
i.vector.8111.070981e-04
i.vector.8411.351654e-04
i.vector.8819.928478e-05
i.vector.8919.801359e-05
i.vector.9019.910171e-05
i.vector.9111.002149e-04
i.vector.9519.821902e-05
i.vector.9919.775918e-05
i.vector.10019.954567e-05
i.vector.10119.820251e-05
i.vector.10411.800451e-04
i.vector.10611.401222e-04
i.vector.10811.248695e-04
In [21]:
classifications<-ifelse(predictionResults$net.result>0.5,1,-1)
classifications
i.vector.71
i.vector.91
i.vector.101
i.vector.111
i.vector.141
i.vector.151
i.vector.171
i.vector.221
i.vector.241
i.vector.251
i.vector.261
i.vector.271
i.vector.281
i.vector.311
i.vector.391
i.vector.411
i.vector.461
i.vector.471
i.vector.511
i.vector.521
i.vector.541
i.vector.561
i.vector.611
i.vector.661
i.vector.721
i.vector.741
i.vector.761
i.vector.801
i.vector.881
i.vector.921
......
i.vector.291-1
i.vector.311-1
i.vector.331-1
i.vector.361-1
i.vector.391-1
i.vector.461-1
i.vector.481-1
i.vector.491-1
i.vector.541-1
i.vector.561-1
i.vector.581-1
i.vector.601-1
i.vector.741-1
i.vector.751-1
i.vector.771-1
i.vector.791-1
i.vector.801-1
i.vector.811-1
i.vector.841-1
i.vector.881-1
i.vector.891-1
i.vector.901-1
i.vector.911-1
i.vector.951-1
i.vector.991-1
i.vector.1001-1
i.vector.1011-1
i.vector.1041-1
i.vector.1061-1
i.vector.1081-1
In [22]:
table(test.set[,class.index],classifications)
    classifications
     -1  1
  -1 51  0
  1   0 44

Classification accuracy: 100%

For a performance comparison on the test set, we will train a second deep neural network with two hidden layers with a 5x4 set of hidden nodes in each layer:

In [23]:
myform <- as.formula(paste('class1.label ~ ',paste(names(training.set[!names(training.set) %in% 'class1.label']), collapse = ' + ')))
twoHiddenLayerClassifier <- neuralnet(myform, training.set, hidden = c(5, 4), rep=500, linear.output = FALSE, threshold = 0.01)
summary(twoHiddenLayerClassifier)
                    Length  Class      Mode    
call                      7 -none-     call    
response                141 -none-     numeric 
covariate            182736 -none-     numeric 
model.list                2 -none-     list    
err.fct                   1 -none-     function
act.fct                   1 -none-     function
linear.output             1 -none-     logical 
data                   1297 data.frame list    
exclude                   0 -none-     NULL    
net.result              500 -none-     list    
weights                 500 -none-     list    
generalized.weights     500 -none-     list    
startweights            500 -none-     list    
result.matrix       3258500 -none-     numeric 
In [24]:
class.index<-length(test.set)
predictionResults<-compute(twoHiddenLayerClassifier,test.set[,-class.index])
predictionResults$net.result
i.vector.70.9890757
i.vector.90.9890757
i.vector.100.9890757
i.vector.110.9890757
i.vector.140.9890757
i.vector.150.9890757
i.vector.170.9890757
i.vector.220.9890757
i.vector.240.9890757
i.vector.250.9890757
i.vector.260.9890757
i.vector.270.9890757
i.vector.280.9890757
i.vector.310.9890757
i.vector.390.9890757
i.vector.410.9890757
i.vector.460.9890757
i.vector.470.9890757
i.vector.510.9890757
i.vector.520.9890757
i.vector.540.9890757
i.vector.560.9890757
i.vector.610.9890757
i.vector.660.9890757
i.vector.720.9890757
i.vector.740.9890757
i.vector.760.9890757
i.vector.800.9890757
i.vector.880.9890757
i.vector.920.9890757
......
i.vector.2916.817724e-08
i.vector.3116.817724e-08
i.vector.3316.817724e-08
i.vector.3616.817724e-08
i.vector.3916.817724e-08
i.vector.4616.817724e-08
i.vector.4816.817724e-08
i.vector.4916.817724e-08
i.vector.5416.817724e-08
i.vector.5616.817724e-08
i.vector.5816.817724e-08
i.vector.6016.817724e-08
i.vector.7416.817724e-08
i.vector.7516.817724e-08
i.vector.7716.817724e-08
i.vector.7916.817724e-08
i.vector.8016.817724e-08
i.vector.8116.817724e-08
i.vector.8416.817724e-08
i.vector.8816.817724e-08
i.vector.8916.817724e-08
i.vector.9016.817724e-08
i.vector.9116.817724e-08
i.vector.9516.817724e-08
i.vector.9916.817724e-08
i.vector.10016.817724e-08
i.vector.10116.817724e-08
i.vector.10416.817724e-08
i.vector.10616.817724e-08
i.vector.10816.817724e-08
In [25]:
classifications<-ifelse(predictionResults$net.result>0.5,1,-1)
classifications
i.vector.71
i.vector.91
i.vector.101
i.vector.111
i.vector.141
i.vector.151
i.vector.171
i.vector.221
i.vector.241
i.vector.251
i.vector.261
i.vector.271
i.vector.281
i.vector.311
i.vector.391
i.vector.411
i.vector.461
i.vector.471
i.vector.511
i.vector.521
i.vector.541
i.vector.561
i.vector.611
i.vector.661
i.vector.721
i.vector.741
i.vector.761
i.vector.801
i.vector.881
i.vector.921
......
i.vector.291-1
i.vector.311-1
i.vector.331-1
i.vector.361-1
i.vector.391-1
i.vector.461-1
i.vector.481-1
i.vector.491-1
i.vector.541-1
i.vector.561-1
i.vector.581-1
i.vector.601-1
i.vector.741-1
i.vector.751-1
i.vector.771-1
i.vector.791-1
i.vector.801-1
i.vector.811-1
i.vector.841-1
i.vector.881-1
i.vector.891-1
i.vector.901-1
i.vector.911-1
i.vector.951-1
i.vector.991-1
i.vector.1001-1
i.vector.1011-1
i.vector.1041-1
i.vector.1061-1
i.vector.1081-1
In [26]:
table(test.set[,class.index],classifications)
    classifications
     -1  1
  -1 51  0
  1   0 44

Classification accuracy: 100%

So, there is no benefit to increasing complexity of the deep neural network for a toy classification problem this small.